<?php
/**
 * Created by PhpStorm.
 * User: hwy
 * Date: 2021/2/14
 * Time: 20:27
 */

namespace App\Http\Controllers\Index;


use App\Exceptions\ApiException;
use App\Handlers\SwooleHandler;
use App\Http\Controllers\Controller;
use App\Jobs\TestJob;
use app\Lib\Org\SwooleMysql;
use App\Models\StockArticle;
use App\Models\Test;
use App\Models\WxArticle;
use App\Services\CollectService;
use App\Services\DingService;
use App\Services\ElasticSearchService;
use App\Services\HttpServer;
use App\Services\StockArticleService;
use App\Services\StockService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Str;

class TestController extends Controller
{
    public function getApiInfo(Request $request)
    {
        $type = $request->type;
        switch ($type) {
            case 1:
                $data = $this->getArticle();
                break;
            case 2:
                $data = $this->getWxArticle();
                break;
            case 3:
                $data = $this->checkSql();
                break;
            case 4:
                $data = $this->getYnData();
                break;
            case 5:
                $data = $this->swoole();
                break;
            case 6:
                $data = StockService::getInstance()->get_stock_real_time(['601318' => '中国平安'], 'http://qt.gtimg.cn/q=');
                dd($data);
            case 7:
                $data = DingService::getInstance()->textMsgSend('发送测试消息', ['18459266204']);
                dd($data);
            case 8:
                $data = StockService::getInstance()->addPeDataReal();
                dd($data);
            case 9:
                $data = StockService::getInstance()->getXueqiuData();
                dd($data);
                break;
            case 10://b站漫画
                $url = 'https://manga.bilibili.com/twirp/activity.v1.Activity/ClockIn';
                $cookie = '1111';
                $data = curl($url,false,1,0,1,['Cookie:'.$cookie]);
                $data = iconv("gb2312", "utf-8//IGNORE", $data);
                $data = json_decode($data, true);
                dd($data);
                break;
            case 11://朴朴签到
                $url = 'https://j1.pupuapi.com/client/account/asserts';
                $url = 'https://j.pupuapi.com/client/game/sign';
                $data = curl($url,false,0,1);
                // $data = iconv("gb2312", "utf-8//IGNORE", $data);
                // $data = json_decode($data, true);
                dd($data);
                break;
            case 12://
                $url = '';
                $data = CollectService::getInstance()->get_test();
                dd($data);
                break;
            default:

                break;
        }

        dd($data);
    }

    //采集各种文章
    public function getArticle()
    {


        $t1             = microtime(true);
        $collect_handle = CollectService::getInstance();


        $collect_handle->collect_36kr();
        $collect_handle->collect_huxiu();
        $collect_handle->collect_tmtpost();
         $res = $collect_handle->addDataDb();

        $t2 = microtime(true);
        $t3 = (($t2 - $t1) * 1000) . 'ms';

        dd($res, $t3);

        // get_sqls();
        // $data_1 = DB::table('article_stock')->where('title', 'like', '%歌尔%')->limit(10)->get();
        // $data_1 = json_decode($data_1, true);
        // $t2     = microtime(true);
        // $t3     = (($t2 - $t1) * 1000) . 'ms';
        //
        // dd($data_1);
        $stock_article = StockArticleService::getInstance();
        $stock_article->collectYbsArticle();
        return '增加文章：';

        //将数据表数据加入 es
        // $t1_1     = microtime(true);
        // $esClient = new ElasticSearchService('ybs_log');
        // $query    = [
        //     'query' => [
        //         'bool' => [
        //             'must' => [
        //                 'match_phrase_prefix' => [
        //                     'title' => '歌尔',
        //                 ],
        //                 // 'match' => [
        //                 //     'title' => [
        //                 //         'query' => '歌尔',
        //                 //         'boost' => 2,
        //                 //     ],
        //                 // ],
        //             ],
        //         ],
        //     ],
        //     //排序有问题，还没找到原因
        //     // 'sort'  => [
        //     //     'upTime' => [
        //     //         'order' => 'desc',
        //     //     ],
        //     // ],
        //
        //
        // ];
        // $data     = $esClient->search($query, 0, 100);
        // $t2_2     = microtime(true);
        // $t3_3     = (($t2_2 - $t1_1) * 1000) . 'ms';
        // dd($data, $t3_3);
        //
        // if(Cache::has('stock_article')){
        //     $data = Cache::get('stock_article');
        // }else{
        //     $data = StockArticle::query()->select('title','created_at','updated_at','type','content','subjectId','summary','readingSum','commentSum','cover','upTime','isOpen','openStick','zlAvatarUrl','zlName','zlSubscribeType','zlType')->get()->toArray();
        //     Cache::put('stock_article',$data,120);
        // }
        //
        // foreach ($data as $val) {
        //     $res = $esClient->putDoc($val);
        // }


    }

    //采集微信文章
    public function getWxArticle()
    {
        $url = WxArticle::getWxArticle();


        $move_url = curl($url);
        //      $preg='/\<a.+?href=(\"|\')(?!javascript:|#)(.+?)(\"|\')/i';
        $preg = '/<a .*?href="(.*?)".*?>/is';
        preg_match_all($preg, $move_url, $matches);

        // $headers = get_headers($matches[1][0], 1);dd($headers);
        // if ($headers && $headers['Location']){
        //     echo $headers['Location'];
        // }

        $data = curl($matches[1][0], false, 0, 0, 3);

        dd($data, $matches[1][0], $url);
    }

    //数据库效率问题
    public function checkSql()
    {
        $t1     = microtime(true);
        $i_data = [
            'name'        => 'qwertyuioplkjhgfdsazxcvbnm',
            'value'       => 'qwertyuioplkjhgfdsazxcvbnmqwertyuioplkjhgfdsazxcvbnmqwsdfghjkljlkhfdasertyuioplkjhgfdsazxcvbnmqwertyuioplkjhgfdsazxcvbnmqwertyuiopljhgfdsazxcvbnm',
            'created_at'  => '2020-10-22 00:00:00',
            'created_at1' => '2020-10-22 00:00:00',
            'created_at1' => '2020-10-22 00:00:00',
            'created_at2' => '2020-10-22 00:00:00',
            'created_at3' => '2020-10-22 00:00:00',
        ];
        //        11.3s
        //        for ($i = 0; $i < 1000; $i++) {
        //            $i_datas = [];
        //            for ($j = 0; $j < 10; $j++) {
        //                $i_datas[] = $i_data;
        //            }
        //
        //            DB::table('test')->insert($i_datas);
        //        }
        ////        0.5s
        //        for ($i = 0; $i < 10; $i++) {
        //            $i_datas = [];
        //            for ($j = 0; $j < 1000; $j++) {
        //                $i_datas[] = $i_data;
        //            }
        //
        //            DB::table('test')->insert($i_datas);
        //        }
        //
        ////        事务  35s
        //        DB::beginTransaction();
        //        for ($i = 0; $i < 100000; $i++) {
        //            $i_datas = $i_data;
        //            DB::table('test')->insert($i_datas);
        //        }
        //        DB::commit();

        //todo 分10次插入，每次1000条，不进行事务   1455ms
        //        for ($i = 0; $i < 10; $i++) {
        //            $i_datas = [];
        //            for ($j = 0; $j < 1000; $j++) {
        //                $i_datas[] = $i_data;
        //            }
        //
        //            DB::table('test')->insert($i_datas);
        //        }
        //todo 分10次插入，每次10000条，不进行事务  14701ms
        //        for ($i = 0; $i < 10; $i++) {
        //            $i_datas = [];
        //            for ($j = 0; $j < 10000; $j++) {
        //                $i_datas[] = $i_data;
        //            }
        //
        //            DB::table('test')->insert($i_datas);
        //        }
        //todo 分10次插入，每次1000条，进行事务 1472ms
        // DB::beginTransaction();
        for ($i = 0; $i < 10; $i++) {
            $i_datas = [];
            for ($j = 0; $j < 1000; $j++) {
                $i_datas[] = $i_data;
            }

            // Test::query()->insert($i_datas);

            dispatch((new TestJob($i_datas))->onQueue('default'));
        }
        // DB::commit();

        //todo 分10次插入，每次10000条，进行事务 15839ms
        //        DB::beginTransaction();
        //        for ($i = 0; $i < 10; $i++) {
        //            $i_datas = [];
        //            for ($j = 0; $j < 10000; $j++) {
        //                $i_datas[] = $i_data;
        //            }
        //
        //            DB::table('test')->insert($i_datas);
        //        }
        //        DB::commit();


        $t2 = microtime(true);
        $t3 = (($t2 - $t1) * 1000) . 'ms';
        dd([4, $t3]);
    }

    //
    public function getYnData()
    {

        StockService::getInstance()->stockYnData(3,'008903');


    }

    public function swoole()
    {
        $t1 = microtime(true);

        // $collect_handle = CollectService::getInstance();
        // $collect_handle->collect_36kr();
        // $collect_handle->collect_huxiu();
        // $collect_handle->collect_tmtpost();
        // $cache_name  = 'collect_article';
        // $data_len    = Redis::llen($cache_name);
        // $insert_data = [];
        // $i           = 1;
        // while ($i <= $data_len) {
        //     $insert_data[] = json_decode(Redis::lPop($cache_name), true);
        //     $i++;
        // }
        // $cc   = '';
        // $list = count($insert_data);

        $i_data = [
            'name'        => 'qwertyuioplkjhgfdsazxcvbnm',
            'value'       => 'qwertyuioplkjhgfdsazxcvbnmqwertyuioplkjhgfdsazxcvbnmqwsdfghjkljlkhfdasertyuioplkjhgfdsazxcvbnmqwertyuioplkjhgfdsazxcvbnmqwertyuiopljhgfdsazxcvbnm',
            'created_at'  => '2020-10-22 00:00:00',
            'created_at1' => '2020-10-22 00:00:00',
            'created_at1' => '2020-10-22 00:00:00',
            'created_at2' => '2020-10-22 00:00:00',
            'created_at3' => '2020-10-22 00:00:00',
        ];
        for ($i = 0; $i < 1; $i++) {
            $i_datas = [];
            for ($j = 0; $j < 1000; $j++) {
                $i_datas[] = $i_data;
            }
            // DB::table('test')->insert($i_datas);

            $server = new HttpServer('0.0.0.0', 5200);
            $server->start();

            // $sql_init = new SwooleMysql();
            // $sql_init->create();
            // $sql_init->table('test');

        }

        $t2 = microtime(true);
        $t3 = (($t2 - $t1) * 1000) . 'ms';
        dd($t3);
    }

}
